package com.lvtongka.app.ui.recharge.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvtongka.app.R
import com.lvtongka.app.databinding.ActivityRechargeItemBinding
import com.lvtongka.app.ui.recharge.bean.ChannelList


class RechargeAdapter(private var itemClickListener: OnItemClickListener?) : RecyclerView.Adapter<RechargeAdapter.RechargeViewHolder>() {

    private var mList = mutableListOf<ChannelList>()

    fun setList(list: List<ChannelList>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    open inner class RechargeViewHolder(var binding: ActivityRechargeItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RechargeViewHolder {
        var binding =  ActivityRechargeItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return RechargeViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: RechargeViewHolder, position: Int) {
        var data = mList[position]
        data?.let {
            holder.binding.tvName.text = it.name
            when (it.payType) {
                "4"->{
                    holder.binding.ivLogo.setImageResource(R.mipmap.icon_alipay)
                }
                "3"->{
                    holder.binding.ivLogo.setImageResource(R.mipmap.icon_digital_yuan)
                }
                "6"->{
                    holder.binding.ivLogo.setImageResource(R.mipmap.icon_e_bank)
                }
            }
            holder.binding.llEBank.setOnClickListener {
                itemClickListener?.onItemClick(data)
            }
        }
    }
}

interface OnItemClickListener {
    fun onItemClick(channel: ChannelList)
}